using System;
using UnityEngine;
using TMPro;
using Utility.GameEvents.Core;

namespace Utility.GameEvents.Examples {
    public class ScoreDisplayUI : MonoBehaviour, IGameEventListener<PlayerScoreChangedEvent> {
        [Header("UI Reference")] [Tooltip("Text component to display the score")] [SerializeField]
        private TextMeshProUGUI scoreText;

        [Header("Settings")] [Tooltip("Which player's score this UI should display.")] [SerializeField]
        private string playerIdToWatch = "Player1";

        private void OnEnable() {
            EventBus.Register<PlayerScoreChangedEvent>(this);
        }

        private void OnDisable() {
            EventBus.Unregister<PlayerScoreChangedEvent>(this);
        }
        
        public void OnEventRaised(PlayerScoreChangedEvent gameEvent) {
            if (gameEvent.PlayerID == playerIdToWatch) {
                if (scoreText) {
                    scoreText.text = $"Player {gameEvent.PlayerID}: {gameEvent.NewScore}";
                }
            }
        }

        public void SetPlayerIdToWatch(string playerId) {
            playerIdToWatch = playerId;
        }
    }
}